import Vue from 'vue'
import App from './App.vue'


import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from "vue-router";
import Router from "vue-router";
import Mock from "mockjs";
import './common/axios'
Vue.use(ElementUI).use(VueRouter)
document.domain="127.0.0.1"
console.log(1,Vue.prototype.$http)

const modulesFiles = import.meta.globEager('./views/*/*.vue')
const routes =  [ { path:'/',	component: () => import("./index.vue") }]
for (const path in modulesFiles) {
	console.log(path);
	routes.push({ path: "/" + path.replace(/(.*\/)*([^.]+).*/ig, "$2"), component: () => import(path) })
}
window.routes = routes

Vue.prototype.toPage = function (url){
	if(url.startsWith("http")) return window.open(url)
	this.$router.push(url)
};

Vue.prototype.fillArray = (len = 100, obj = {}) => {
	return Array(len).fill(obj).map((e, i) => { e.id = i; return e })
};
Vue.prototype.fillArrays = (len = 100) => {
	var arr = Array(len)
	for (var i = 0; i < len; i++) {
		arr.push({
			id: i,
			id: 1100 + i,
			name: Mock.mock('@ctitle(2, 5)'),
			title: Mock.mock('@ctitle(10, 20)'),
			content: Mock.mock('@ctitle(10, 50)'),
			like: Mock.mock('@natural(60, 100)'),
			love: Mock.mock('@natural(60, 100)'),
			date: Mock.mock('@now'),
			img: `https://via.placeholder.com/${Mock.mock('@natural(60, 100)')}x${Mock.mock('@natural(100, 300)')}.png`,
			age: ~~(Math.random() * 100),
		})
	}
	return arr
};

Vue.prototype.list = Vue.prototype.fillArray(1000)

const router =  new Router({ routes})

router.beforeEach((to, from, next) => {
	if (['/home','/upload'].includes(to.path)) {
		if (!localStorage.token) {
			return next({ path: '/login',query: {redirect:to.fullPath}, })     
		}
	}
	return	next()
})

new Vue({
	router,
	render: h => h(App)
}).$mount('#app')